package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.MktRewardMapper;
import com.ruoyi.system.domain.MktReward;
import com.ruoyi.system.service.IMktRewardService;

@Service
public class MktRewardServiceImpl extends ServiceImpl<MktRewardMapper, MktReward> implements IMktRewardService {

    /**
     * 查询营销奖励管理列表
     *
     * @param mktReward 营销奖励管理
     * @return 营销奖励管理
     */
    @Override
    public List<MktReward> selectMktRewardList(MktReward mktReward) {
        return getBaseMapper().selectMktRewardList(mktReward);
    }

    @Override
    public boolean save(MktReward entity) {
        //先根据拿到的营销活动的id查询出当前营销活动的各个奖励的分布
        LambdaQueryWrapper<MktReward> queryWrapper = new LambdaQueryWrapper<MktReward>()
                .eq(MktReward::getActId, entity.getActId());
        List<MktReward> list = super.list(queryWrapper);
        //拿到每一个奖励的概率
        //定义总概率
        float total = 0;
        for (MktReward mktReward : list) {
            total+= mktReward.getProbability();
        }

        if (total + entity.getProbability() > 100){
            throw new ServiceException("概率总和不得操过100%");
        }
        return super.save(entity);
    }
}